package com.caizongzhen.www.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.caizongzhen.www.entity.FeedBack;
import com.caizongzhen.www.entity.Illness;
import com.caizongzhen.www.mapper.IllnessKindMapper;
import com.caizongzhen.www.mapper.IllnessMapper;
import com.caizongzhen.www.mapper.PageViewMapper;
import com.caizongzhen.www.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class IllnessService  extends ServiceImpl<IllnessMapper, Illness> {
    @Autowired
    private PageViewService pageViewService;
    @Autowired
    private IllnessKindService illnessKindService;
    public static int total;

    public List<Illness> getIllnessDateByIllnessKind(Integer kind){
        QueryWrapper<Illness> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("kind_id",kind);
        List<Illness> illnessList = this.baseMapper.selectList(queryWrapper);
        if (illnessList.size() > 0) {
            illnessList.forEach(s -> {
                s.setKindName(illnessKindService.getById(s.getKindId()).getName());
                QueryWrapper queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("illness_id",s.getId());
                s.setPageView(pageViewService.getOne(queryWrapper1).getPage_views());
            });
        }
        return illnessList;
    }
    public List<Illness> getIllnessPageDateByIllnessName(String illnessName, Integer pageSize, Integer currentPage){
        List<Illness> illnessList = new ArrayList<>();
        if (Assert.isEmpty(illnessName)) {
            Page<Illness> illnessPage = new Page<Illness>(currentPage,pageSize);
            IPage<Illness> illnessPage1 =this.page(illnessPage);
            // 获取分页数据
            illnessList = illnessPage.getRecords();
            total = (int) illnessPage.getTotal();
        }
        else {
            // 模糊查找
            QueryWrapper<Illness> queryWrapper = new QueryWrapper<>();
            queryWrapper.like(illnessName!=null,"illness_name",illnessName);
            illnessList = this.baseMapper.selectList(queryWrapper);
        }
        if (illnessList.size() > 0) {
            total =  (int) illnessList.size();
            illnessList.forEach(s -> {
                s.setKindName(illnessKindService.getById(s.getKindId()).getName());
                QueryWrapper queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("illness_id",s.getId());
                s.setPageView(pageViewService.getOne(queryWrapper1).getPage_views());
            });
        }
        return illnessList;
    }
    public List<Illness> getIllnessPageDate(Integer currentPage, Integer pageSize){
        Page<Illness> illnessPage = new Page<Illness>(currentPage,pageSize);
        List<Illness> illnessList =this.page(illnessPage).getRecords();
        if (!Assert.isEmpty(illnessList) || illnessList.size() > 0) {
            illnessList.forEach(s -> {
                s.setKindName(illnessKindService.getById(s.getKindId()).getName());
                QueryWrapper queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("illness_id",s.getId());
                s.setPageView(pageViewService.getOne(queryWrapper1).getPage_views());
            });
        }
        return illnessList;
    }
}
